package cn.iocoder.yudao.module.system.service.a_manuscriptLog;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.a_manuscriptLog.vo.ManuscriptPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.a_manuscriptLog.ManuscriptLogDO;
import cn.iocoder.yudao.module.system.config.a_gaojian.ConnectionConfig;
import com.trs.hybase.client.*;
import com.trs.hybase.client.params.SearchParams;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lxj
 */
@Service
@Validated
public class ManuscriptServiceLogImpl implements ManuscriptLogService {
    /**
     * 稿件日志表名
     */
    @Value("${hybase.dbname.manuscriptLog}")
    private String dbName;


    @Override
    public void insertManscriptorLog(TRSConnection conn, ManuscriptLogDO manuscriptDO) {
        //创建记录对象
        List<TRSInputRecord> recordList = new ArrayList<>();
        TRSInputRecord record = new TRSInputRecord();
        //添加字段
        try {
            record.addColumn("relevanceId", manuscriptDO.getRelevanceId());
            record.addColumn("version", manuscriptDO.getVersion());
            record.addColumn("create_user_id", manuscriptDO.getCreateUserId());
            record.addColumn("create_time", manuscriptDO.getCreateTime());
            record.addColumn("filePath", manuscriptDO.getFilePath());
            recordList.add(record);
            //执行插入记录（暂时在内存中）
            conn.executeInsert(dbName, recordList);
        } catch (TRSException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Boolean findExistByRelevanceId(TRSConnection conn, String relevanceId) {
        //设置返回结果字段
        TRSResultSet resultSet;
        String whereStr = "relevanceId:" + relevanceId;
        try {
            //在"pictures"数据库里检索
            resultSet = conn.executeSelect(dbName, whereStr, 0, 1, new SearchParams());
            if (resultSet != null) {
                if (resultSet.getNumFound() > 0) {
                    return true;
                }
            }
        } catch (TRSException e) {
            System.out.println("ErrorCode:" + e.getErrorCode());
            System.out.println("ErrorString:" + e.getErrorString());
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        return false;
    }

    @Override
    public PageResult<ManuscriptPageReqVO> findPageByRelevanceId(ManuscriptPageReqVO pageReqVO) {
        PageResult<ManuscriptPageReqVO> pageResult = new PageResult<>();
        List<ManuscriptPageReqVO> manuscriptLogDOList = new ArrayList<>();
        //创建与服务器的连接实例
        TRSConnection conn = ConnectionConfig.getTRSConnection();
        SearchParams param = new SearchParams();
        //设置结果排序方式为创建时间排序
        param.setSortMethod("-create_time");
        //设置返回结果字段
        param.setReadColumns("relevanceId;version;filePath;createUserId;createTime");
        TRSResultSet resultSet;
        Long userId = WebFrameworkUtils.getLoginUserId();
        String whereStr = "relevanceId:" + pageReqVO.getRelevanceId();

        try {
            //在"pictures"数据库里检索
            resultSet = conn.executeSelect(dbName, whereStr, ((pageReqVO.getPageNo() - 1) * pageReqVO.getPageSize()), pageReqVO.getPageSize(), param);
            if (resultSet != null) {
                for (int i = 0; i < resultSet.size(); i++) {
                    resultSet.moveNext();
                    TRSRecord re = resultSet.get();
                    ManuscriptPageReqVO manuscriptPageReqVO = new ManuscriptPageReqVO();
                    manuscriptPageReqVO.setId(re.getUid());
                    manuscriptPageReqVO.setRelevanceId("relevanceId");
                    manuscriptPageReqVO.setVersion(re.getInt("version"));
                    manuscriptPageReqVO.setFilePath(re.getString("filePath"));
                    manuscriptPageReqVO.setCreateTime(re.getString("createTime"));
                    manuscriptPageReqVO.setCreateUserId(re.getString("createUserId"));
                    manuscriptLogDOList.add(manuscriptPageReqVO);
                }

                pageResult.setTotal(resultSet.getNumFound());
                pageResult.setList(manuscriptLogDOList);

            }
        } catch (TRSException e) {
            System.out.println("ErrorCode:" + e.getErrorCode());
            System.out.println("ErrorString:" + e.getErrorString());
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        return pageResult;
    }


}
